import {
	getAllSalary,
	saveSalary,
	submitSalary,
	getUserList,
	getUserSalary,
	getSubmitSalary,
	agreeSalary
} from '@/api'

const state = {
	userList: []
}

const mutations = {
	getUserList(state, arr){
		state.userList = arr
	}
}

const actions = {
	// 查询工资列表(待提交，已提交)
	async getAllSalary({commit}, data){
		let res = await getAllSalary(data)
		return res
	},
	// 保存工资条
	async saveSalary({commit}, data){
		let res = await saveSalary(data)
		return res
	},
	// 提交工资条
	async submitSalary({commit}, data){
		let res = await submitSalary(data)
		return res
	},
	// 请求部门，员工信息
	async getUserList({commit}){
		let res = await getUserList()
		let index = 0
		let arr = []
		res.departmentArr.forEach(departmentArrItem => {
			arr[index] = {}
			arr[index].label = departmentArrItem.department_name
			arr[index].value = departmentArrItem.department_id.toString()
			arr[index].children = []
			index++
		})
		index = 0
		arr.forEach(optionItem => {
			res.data.map(item => {
				if (item.department_id == optionItem.value){
					optionItem.children[index] = {}
					optionItem.children[index].label = item.name
					optionItem.children[index].value = optionItem.value + '.' + item.id
					index++
				}
			})
			index = 0
		})
		commit('getUserList', arr)
	},
	// 获取个人工资信息
	async getUserSalary({commit}, data){
		let res = await getUserSalary(data)
		return res
	},
	// 获取待审核和已审核的工资记录
	async getSubmitSalary({commit}){
		let res = await getSubmitSalary()
		return res
	},
	// 提交工资条
	async agreeSalary({commit}, data){
		let res = await agreeSalary(data)
		return res
	},
}

export default {
	namespaced: true,
	state,
	mutations,
	actions
}
